<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;

class Withdraw extends Model
{
    // 表名
    protected $name = 'withdraw';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'status_text',
        'handle_time_text'
    ];
    

    const STATUS_W = 0;
    const STATUS_Y = 1;
    const STATUS_N = 2;
    
    public static $_status = [
        self::STATUS_W => '待处理',
	    self::STATUS_Y => '已转帐',
	    self::STATUS_N => '已退款'
    ];
    
    public function getStatusList()
    {
        return self::$_status;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHandleTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['handle_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setHandleTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
	
	public function user() {
		return $this->belongsTo('User', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function userVerified(){
		return $this->belongsTo('userVerified', 'user_id', 'user_id', '', 'left')->setEagerlyType(0);
	}
	
	
	public function deny($id) {
    	$row = $this->find($id);
		if(!$row) {
			$this->error = '记录不存在';
			return false;
		}
		
		if($row['status'] != self::STATUS_W) {
			$this->error = '非待审核记录';
			return false;
		}
		
		$this->startTrans();
		try {
			
			//更改记录状态
			$row['status'] = self::STATUS_N;
			$res = $row->save();
			if(!$res) {
				throw new Exception('更改记录状态出错');
			}
			
			//加钱
			$userRes = Db::name('user')->where(['id' => $row['user_id']])->setInc('balance', $row['amount']);
			if(!$userRes) {
				throw new Exception('添加用户资金出错');
			}
			
			$user = Db::name('user')->find($row['user_id']);
			//记录流水
			$logModel = new FundsRecord();
			$logRes = $logModel->addRecord($user['id'],$row['amount'], $user['balance'],$logModel::ACCOUNT_TYPE_BALANCE, $logModel::OPE_TYPE_PLUS, $logModel::TYPE_WITHDRAW_RETURN,'','提现退款');
			if(!$logRes) {
				throw new Exception('添加资金流水出错');
			}
			
			$this->commit();
			return true;
		} catch (Exception $e) {
			$this->error = $e->getMessage();
			$this->rollback();
			return false;
		}
	}
}
